#!/usr/bin/env python
# vim:fileencoding=utf-8
from __future__ import with_statement, unicode_literals
from calibre.web.feeds.news import BasicNewsRecipe


class AdvancedUserRecipe1592177429(BasicNewsRecipe):
    title = 'Аргументы и Факты'
    encoding = 'utf8'
    language = 'ru'
    oldest_article = 7
    max_articles_per_feed = 25
    verbose = 3

    feeds = [
        ('AIF', 'https://www.aif.ru/rss/all.php'),
    ]
    INDEX = 'https://www.aif.ru/rss/all.php'

    def parse_index(self):
        feeds = []
        section_title = 'aif'
        articles = []
        soup = self.index_to_soup(self.INDEX)
        ii = 0
        for item in soup.findAll('item'):
            if ii < self.max_articles_per_feed:
                try:
                    ii = ii + 1
                    A = str(item)
                    i = A.find(u'link')
                    j = A.find(u'description')
                    ZZ = item.find('description')
                    ZZ1 = str(ZZ)  # bs4.element.Tag to str
                    ZZ2 = ZZ1[24:-19]
                    AU = A[i:j]
                    try:
                        articles.append({'url':AU[6:-2], 'title':ZZ2})
                    except Exception:
                        pass
                except Exception:
                    self.log("Exception handled!")
        if articles:
            feeds.append((section_title, articles))
        return feeds
